<?php

namespace app\api\controller\app;

use app\admin\model\Banner;
use app\admin\model\City;
use app\admin\model\Org;
use app\admin\model\OrgIndexNotice;
use app\admin\model\OrgNotice;
use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopdaytrade;
use app\admin\model\Shopdevice;
use app\admin\model\Shopmno;
use app\admin\model\Shoporder;
use app\admin\model\Shoporderprofit;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Userbalance;
use app\admin\model\Userorderprofit;
use app\admin\model\Usertuan;
use app\admin\model\Userweixinmp;
use app\api\service\DeviceService;
use app\api\service\MeituanService;
use app\api\service\MpService;
use app\api\service\MtService;
use app\api\service\MtUtil;
use app\api\service\VbillService;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Http;
use fast\Random;
use think\Config;
use think\Validate;

/**
 */
class Task extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

    }

    public function updateMtToken()
    {

        $shopModel = new Shop();
        $time = strtotime("+1 day");
        $list = $shopModel->where('mt_update_time', "<", $time)->where('mt_token', '<>', '')->where('mt_refresh_token', "<>", '')->select();

        $mtService = new MtService();
        foreach ($list as $key => $val) {


            if ($val['is_sync'] == 1) {
                $req = array();
                $req['shop_id'] = $val['id'];
                $mtService->refreshToken($req);
            }

        }
        $this->success('成功', $list);

    }


    public function refreshMeituanToken()
    {

        $shopModel = new Shop();
        $time = strtotime("+1 day");
        $list = $shopModel->where('mt2_update_time', "<", $time)->where('mt2_token', '<>', '')->where('mt2_refresh_token', "<>", '')->select();

        $meituanService = new MeituanService();
        foreach ($list as $key => $val) {

            $req = array();
            $req['shop_id'] = $val['id'];
            $req['refreshToken'] = $val['mt2_refresh_token'];
            $meituanService->refreshToken($req);

        }
        $this->success('成功', $list);

    }


    public function updateBookTask()
    {

        $endTime = strtotime("-3 minute", time());
        $shopOrderBookModel = new Shoproombook();
        $shopCleanModel = new Roomclean();
        $list = $shopOrderBookModel->where('end_time', "<=", $endTime)->where('status', 'in', ['doing', 'undo'])->order('end_time', 'desc')->limit(10)->select();
        $shopOrderModel = new Shoporder();
        $shopRoomModel = new Shoproom();
        foreach ($list as $key => $val) {
            $shopOrderModel->where('id', $val['order_id'])->update(['status' => 'done']);

            $endTime = strtotime("+30 minute", $val['end_time']);
            if (time() >= $endTime) {
                $shopCleanModel->where('order_id', $val['order_id'])->update(['status' => 'done']);
            }
            $shopOrderBookModel->where('id', $val['id'])->update(['status' => 'done']);
            $shopRoomModel->where('id', $val['room_id'])->update(['status' => 'free']);

        }
        $this->success('', $list);
    }


    public function updateBookTask2()
    {

        $endTime = strtotime("-3 minute", time());
        $shopOrderBookModel = new Shoproombook();
        $shopCleanModel = new Roomclean();
        $list = $shopOrderBookModel->where('end_time', "<=", $endTime)->where('status', 'in', ['doing', 'undo'])->order('end_time', 'desc')->limit(10)->select();

        $this->success('', $list);
    }

    public function updateBookTask3()
    {

        $endTime = strtotime("-3 minute", time());
        $shopOrderBookModel = new Shoproombook();
        $shopCleanModel = new Roomclean();
        $list = $shopOrderBookModel->where('end_time', "<=", $endTime)->where('status', 'in', ['doing', 'undo'])->order('end_time', 'desc')->limit(10)->select();
        $shopOrderModel = new Shoporder();
        $shopRoomModel = new Shoproom();
        foreach ($list as $key => $val) {
            $shopOrderModel->where('id', $val['order_id'])->update(['status' => 'done']);
            $is_clean = false;
            $clean = $shopCleanModel->where('order_id', $val['order_id'])->find();

            $endTime = strtotime("+30 minute", $val['end_time']);
            if (time() >= $endTime) {
                $is_clean = true;
                $shopCleanModel->where('order_id', $val['order_id'])->update(['status' => 'done']);
            }
            $shopOrderBookModel->where('id', $val['id'])->update(['status' => 'done']);
            $shopRoomModel->where('id', $val['room_id'])->update(['status' => 'free']);

            if ($is_clean && $clean['user_id'] > 0) {
                $order = $shopOrderModel->where('id', $val['order_id'])->find();
                $mpWxModel = new MpService();
                $userWxMpModel = new Userweixinmp();
                $userWx = $userWxMpModel->where('user_id', $clean['user_id'])->find();
                if($userWx){
                    $msg = array();
                    $msg['openid'] = $userWx['openid'];
                    $msg['shop'] = $order['shop_name'];
                    $msg['room'] = $order['room_name'];
                    $msg['time1'] = date("Y-m-d H:i", $order['book_start']);
                    $msg['time2'] = date("Y-m-d H:i", $order['book_end']);

                    $mpWxModel->cleanStartNotify($this->wxApp, $msg);
                }
            }
        }
        $this->success('', $list);
    }


    public function doOrderTask(DeviceService $deviceService)
    {


        //error_log("====doOrderTask======".date("Y-m-d H:i:s"));
        $shopOrderModel = new Shoporder();
        $shopOrderBookModel = new Shoproombook();
        $shopRoomModel = new Shoproom();
        $deviceModel = new Shopdevice();
        $list = $shopOrderModel->where('org_id', 76)->where('status', 'doing')->order('book_end', 'asc')->limit(50)->select();
        foreach ($list as $key => $val) {

            if ($val['book_end'] <= time()) {
                //关闭订单
                $shopOrderModel->where('id', $val['id'])->update(['status' => 'done']);
                $shopOrderBookModel->where('order_id', $val['id'])->update(['status' => 'done']);

                $shopRoomModel->where('id', $val['room_id'])->update(['status' => 'free', 'is_clean' => -1]);


                $device = $deviceModel->where('room_id', $val['room_id'])->where('device_type', 'qp')->find();

                $doorReq = array();
                $doorReq['open'] = 0;
                $doorReq['open2'] = 0;
                $doorReq['open3'] = 0;
                $doorReq['open4'] = 0;
                $doorReq['device_sn'] = $device['device_sn'];

                $timeReq = array();
                $timeReq['device_sn'] = $device['device_sn'];
                $timeReq['sec'] = 0;
                $timeReq['time'] = time();
                $deviceService->dxIotDoor($doorReq);
                $deviceService->dxIotTime($timeReq);


            }

        }
    }


}
